<template>
  <div class="main_box">
    <el-row>
      <!-- 导航条 -->
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <!-- 头部工具条 -->
      <el-col :span="12">
        <!--<div class="opration_wrap" v-has="permissionAdd">-->
        <!--<el-button type="success" icon="fa fa-plus" @click="handleClickAdd()">新增</el-button>-->
        <!-- <el-button type="success" icon="el-icon-refresh-right" @click="reload">刷新</el-button> -->
        <!--&lt;!&ndash;<span class="line">|</span>&ndash;&gt;-->
        <!--&lt;!&ndash;<el-button type="primary" icon="fa fa-mail-forward">导出</el-button>&ndash;&gt;-->
        <!--</div>-->
      </el-col>
    </el-row>

    <!-- 查询工具条 -->
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="filters" ref="searchForm" class="demo-form-inline">
          <!-- <el-form-item>
            <el-input v-model="formGoodsSearch.goodsId" placeholder="商品编号"></el-input>
          </el-form-item>-->
          <el-form-item prop="goodsName">
            <el-input v-model="filters.goodsName" placeholder="产品名称"></el-input>
          </el-form-item>
          <el-form-item prop="institutionName">
            <el-input v-model="filters.institutionName" placeholder="医院名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="listGoods">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="goodsList" :fit="true" highlight-current-row>
        <el-table-column prop="skuId" align="center" label="产品编码" width="100"></el-table-column>
        <el-table-column label="产品封面" align="center" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.goodsImageOneUrl" class="img_cover" />
          </template>
        </el-table-column>
        <el-table-column prop="goodsName" align="center" width="380" label="产品名称"></el-table-column>
        <el-table-column prop="institutionName" align="center" width="180" label="医院名称"></el-table-column>
        <el-table-column prop="classOneName" align="center" width="180" label="产品类型"></el-table-column>
        <el-table-column label="价格" width="120">
          <template slot-scope="scope">
            <span>￥{{ scope.row.retailPrice }}</span> ~ <span>￥{{ scope.row.platformPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sellNum" align="center" width="100" label="销量"></el-table-column>
        <el-table-column prop="upState" align="center" width="100" label="上架状态" :formatter="formatUpState"></el-table-column>
        <el-table-column prop="upDate" align="center" width="180" label="上架时间"></el-table-column>
        <el-table-column prop="downDate" align="center" width="180" label="下架时间"></el-table-column>
        <el-table-column label="操作" align="center" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-button v-has="permissionUpStatus" @click="handleClickDelete(scope.row)" size="mini">下架</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Dialog from '@/components/Dialog';
import Crumbs from '@/components/Crumbs';
import * as validators from '@/utils/validators';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import { mapGetters, mapActions } from 'vuex';

export default {
  // inject: ['reload'], //注入局部刷新
  data() {
    let _this = this;
    return {
      goodsList: [],
      filters: {
        goodsName: '',
        institutionName: '',
        approveState: null,
        upState: 1,
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
        currentPage: 1,
      },
      reviewStatue: [
        { label: '全选', value: '' },
        { label: '审核驳回', value: -1 },
        { label: '待审核', value: 0 },
        { label: '审核通过', value: 1 },
      ],
      upStatue: [
        { label: '全选', value: '' },
        { label: '下架', value: '-1' },
        { label: '待上架', value: '0' },
        { label: '上架', value: '1' },
        { label: '过期下架', value: '2' },
      ],

      permissionAdd: 'SYSTEM_MANAGER_GOOD_GOOD_SALE_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_GOOD_GOOD_SALE_LIST_SEE',
      permissionUpStatus: 'SYSTEM_MANAGER_GOOD_GOOD_SALE_LIST_DOWN',
    };
  },
  computed: {
    ...mapGetters([]),
  },
  components: {
    Crumbs,
  },
  mounted() {
    let _this = this;
    _this.listGoods();
    _this.getTableHeight();
  },
  methods: {

    formatSex(row, column, cellValue) {
      return cellValue == 1 ? '男' : '女';
    },
    formatApproveState(row, column, cellValue) {
      if (cellValue == -1) {
        return '审核驳回';
      } else if (cellValue == 0) {
        return '待审核';
      } else if (cellValue == 1) {
        return '审核通过';
      }
    },
    formatUpState(row, column, cellValue) {
      if (cellValue == -1) {
        return '下架';
      } else if (cellValue == 0) {
        return '待上架';
      } else if (cellValue == 1) {
        return '上架';
      } else if (cellValue == 2) {
        return '过期下架';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.page.pageNo = 1;
      this.listGoods();
    },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd');
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listGoods();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listGoods();
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/user/GoodsEdit',
        query: { id: row.goodsId },
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_TO_LOWER_FRAME, message.TITLE_PROMPT, {}).then(() => {
        _this
          .$Put(_this.$Config.api.goodsUpOrDown, {
            goodsId: val.goodsId,
            upState: -1,
          })
          .then((res) => {
            _this.listGoods();
          })
          .catch((error) => {
            console.error(error);
          });
      });
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/goods');
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/good/goodSaleDetail',
        query: { id: row.goodsId },
      });
    },
    listGoods() {
      let _this = this;
      //page + condition
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        goodsName: _this.filters.goodsName,
        institutionName: _this.filters.institutionName,
        approveState: _this.filters.approveState,
        upState: _this.filters.upState,
      };
      console.log(cond);
      this.$emit('show-loading', true);
      _this
        .$Get(_this.$Config.api.goodList, cond)
        .then((res) => {
          _this.goodsList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.page.currentPage = res.data.pageNo;
          _this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          _this.$emit('show-loading', false);
        });
    },
    sureGoodsClick() {
      _this.$refs.goodsForm.validate((valid) => {
        if (valid) {
          _this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {}).then(() => {
            // //debugger
            if (_this.goodsForm.goodsId) {
              _this.alterGoods();
            } else {
              _this.addGoods();
            }
          });
        }
      });
    },
    alterGoods() {
      let _this = this;
      _this
        .$Put(_this.$Config.api.goods, _this.goodsForm)
        .then((data) => {
          this.dialogVisible = false;
          _this.listGoods();
          _this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ALTER_SUCCESS,
          });
        })
        .catch((error) => {
          if (error.msg) {
            _this.$notify.error({
              title: message.TITLE_ERROR,
              message: error.msg,
            });
          }
        });
    },
    addGoods() {
      let _this = this;
      _this
        .$Post(_this.$Config.api.goods, _this.goodsForm)
        .then((data) => {
          this.dialogVisible = false;
          _this.listGoods();
          _this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ALTER_SUCCESS,
          });
        })
        .catch((error) => {
          if (error.msg) {
            _this.$notify.error({
              title: message.TITLE_ERROR,
              message: error.msg,
            });
          }
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
